package com.example.wujinapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.wujinapi.comm.Constant;
import com.example.wujinapi.comm.RedisUtils;
import com.example.wujinapi.entity.*;
import com.example.wujinapi.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 会员信息表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-11-17 11:25:35
 */

/*需要注意：必须使用@Controller，不能使用@RestController。
因为@RestController返回的是  JSON，且不走SpringMVC的视图解析流程，所以跳不到html那里。*/
@Controller
@Slf4j
@RequestMapping("/bg")
public class BackgroundController {

    //ajax主机IP地址
    public static final String ServerIP= Constant.ServerIP;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private FirsttypeService firsttypeService;

    @Resource
    private SecondtypeService secondtypeService;

    @Resource
    private ThirdtypeService thirdtypeService;

    @Resource
    private NoticsmessageService noticsmessageService;

    @Resource
    private ContactsService contactsService;

    @Resource
    private ProductsService productsService;

    @Resource
    private PcadvertisingService pcadvertisingService;


    @GetMapping(value = {"/templateTop","/templateTop.html"})
    public String templateTop(Model model) {
        return "bg/templateTop";
    }

    @GetMapping(value = {"/templateLeft","/templateLeft.html"})
    public String templateLeft(Model model) {
        return "bg/templateLeft";
    }

    @GetMapping(value = {"/login","/login.html"})
    public String login(Model model) {
        model.addAttribute("ServerIP",ServerIP);
        return "bg/login";
    }

    /*homePage公共部分*/
    @GetMapping(value = {"/index","/index.html"})
    public String homePage(Model model) {
        model.addAttribute("ServerIP",ServerIP);
        model.addAttribute("pageName","首页");
        return "bg/index";
    }

    //添加主类
    @GetMapping(value = {"/addFirstClass","/addFirstClass.html"})
    public String addFirstClass(Model model) {
        model.addAttribute("ServerIP",ServerIP);
        model.addAttribute("pageName","添加主类");
        return "bg/addFirstClass";
    }


    //管理主类
    @GetMapping(value = {"/mFirst","/mFirst.html"})
    public String mFirst(Model model) {
        model.addAttribute("ServerIP",ServerIP);
        model.addAttribute("pageName","主类列表");
        List<Firsttype> firstClassData=firsttypeService.getFirsttypes();
        model.addAttribute("firstClassData",firstClassData);
        return "bg/mFirst";
    }

    //添加二级类
    @GetMapping(value = {"/addSecondClass","/addSecondClass.html"})
    public String addSecondClass(Model model) {
        model.addAttribute("ServerIP",ServerIP);
        model.addAttribute("pageName","添加二级分类");
        //获取主类
        List<Firsttype> FirstTyps=firsttypeService.getFirsttypes();
        model.addAttribute("FirstType",FirstTyps);
        return "bg/addSecondClass";
    }

    //管理二级类
    @GetMapping(value = {"/mSecond","/mSecond.html"})
    public String mSecond(Model model) {
        model.addAttribute("ServerIP",ServerIP);
        model.addAttribute("pageName","编辑二级分类");
        List<JSONObject> outPutList=new ArrayList<>();
        List<Secondtype> secondClassData=secondtypeService.getSecondtypes();
        secondClassData.forEach(item->{
            JSONObject TempUsers= JSON.parseObject(JSON.toJSONString(item));
            TempUsers.put("firstphotoface",firsttypeService.getFirsttypeById(item.getFirstid()).getFirstphotoface());
            outPutList.add(TempUsers);
        });
        //System.out.println("管理二级类"+outPutList);
        model.addAttribute("secondClassData",outPutList);
        return "bg/mSecond";
    }


    //添加三级类
    @GetMapping(value = {"/addThirdClass","/addThirdClass.html"})
    public String addThirdClass(Model model) {
        model.addAttribute("ServerIP",ServerIP);
        model.addAttribute("pageName","添加三级级分类");
        //获取主类
        List<Firsttype> FirstTyps=firsttypeService.getFirsttypes();
        //FirstTyps.forEach(System.out::println);
        //获取二级分类
        List<Secondtype> SecondTyps=secondtypeService.getSecondtypes();
        //SecondTyps.forEach(System.out::println);
        model.addAttribute("FirstType",FirstTyps);
        model.addAttribute("Secondtype",SecondTyps);
        return "bg/addThirdClass";
    }
    //管理三级类
    @GetMapping(value = {"/mThird","/mThird.html"})
    public String mThird(Model model) {
        model.addAttribute("ServerIP",ServerIP);
        model.addAttribute("pageName","编辑三级分类");
        List<JSONObject> outPutList=new ArrayList<>();
        List<Thirdtype> thirdClassData=thirdtypeService.getThirdtypes();
        thirdClassData.forEach(item->{
            JSONObject TempUsers= JSON.parseObject(JSON.toJSONString(item));
            TempUsers.put("firstphotoface",firsttypeService.getFirsttypeById(item.getFirstid()).getFirstphotoface());
            outPutList.add(TempUsers);
        });
        //System.out.println("管理三级类"+outPutList);
        model.addAttribute("thirdClassData",outPutList);
        return "bg/mThird";
    }
    //添加产品
    @GetMapping(value = {"/addProducts","/addProducts.html","/addProducts/{id}"})
    public String addProducts(Model model,@PathVariable(value = "id",required = false) Integer id) {
        model.addAttribute("ServerIP",ServerIP);
        model.addAttribute("pageName","添加产品");

        //主类
        List<Firsttype> firsttypes=firsttypeService.getFirsttypes();
        model.addAttribute("FirstType",firsttypes);

        //id不为空,查询产品数据
        Products products=null;
        if(!Objects.isNull(id)){
            products=productsService.getProductById(id);
            //格式化为json格式，并将双引号变为单引号
            JSONObject TempDatas= JSON.parseObject(JSON.toJSONString(products));
            model.addAttribute("products",TempDatas.toJSONString());
        }else{
            model.addAttribute("products",null);
        }


        return "bg/addProducts";
    }


    //添加产品规格
    @GetMapping(value = {"/addProductSpec","/addProductSpec.html","/addProductSpec/{id}"})
    public String addProductSpec(Model model,@PathVariable(value = "id",required = false) Integer id) {
        model.addAttribute("ServerIP",ServerIP);
        model.addAttribute("pageName","添加产品规格");
        //id不为空,查询产品数据
        Products products=null;
        if(!Objects.isNull(id)){
            products=productsService.getProductById(id);
            //格式化为json格式，并将双引号变为单引号
            JSONObject TempDatas= JSON.parseObject(JSON.toJSONString(products));
            model.addAttribute("products",TempDatas.toJSONString());
        }else{
            model.addAttribute("products",null);
        }
        return "bg/addProductSpec";
    }


    //产品管理
    @GetMapping(value = {"/mPdList","/mPdList.html","/mPdList-{page}.html","/mPdList-{page}"})
    public String mPdList(Model model,@PathVariable(value = "page",required = false) String page) {

        //获取页数
        int pageNum=(page!=null && !StringUtils.isEmpty(page))?Integer.valueOf(page):1;

        //获取产品列表信息
        List<Map<String, Object>> productLists=productsService.getProducts(pageNum,Constant.PAGESIZE);

        int total=productsService.getAllProductsTotals();
        float num=total/Constant.PAGESIZE;
        //System.out.println("总条目数:"+total+","+Constant.PAGESIZE+","+num);
        int pageCount=(total/Constant.PAGESIZE)+((total%Constant.PAGESIZE)>0?1:0); //  //计算总页数
        if(pageCount<=0) pageCount=1;
        //System.out.println("总页数:"+pageCount);

        model.addAttribute("ServerIP",ServerIP);
        model.addAttribute("pageName","产品列表");
        model.addAttribute("pageIndex",pageNum);
        model.addAttribute("pageCount",pageCount);
        model.addAttribute("productLists",productLists);

        return "bg/mPdList";
    }



    //添加联系方式
    @GetMapping(value = {"/addContacts","/addContacts.html"})
    public String addContacts(Model model) {
        model.addAttribute("ServerIP",ServerIP);
        model.addAttribute("pageName","添加联系方式");

        //读取内容
        Contacts contacts=contactsService.getContactNotNull();
        String contentVal=contacts.getContent();
        model.addAttribute("contentVal",contentVal);
        return "bg/addContacts";
    }
    //管理联系方式列表
    /*@GetMapping(value = {"/contact","/contact.html"})
    public String contact(Model model) {
        model.addAttribute("ServerIP",ServerIP);
        model.addAttribute("pageName","联系方式");
        //主类
        List<Contacts> contacts=contactsService.getContacts();
        model.addAttribute("contactsData",contacts);
        return "bg/contact";
    }*/




    /*首页广告*/
    @GetMapping(value = {"/ad","/ad.html"})
    public String ad(Model model) {
        model.addAttribute("pageName","首页广告图&视频");
        model.addAttribute("ServerIP",ServerIP);

        Pcadvertising ad1=pcadvertisingService.ReadPcadBySortNum(1);
        Pcadvertising ad2=pcadvertisingService.ReadPcadBySortNum(2);
        Pcadvertising ad3=pcadvertisingService.ReadPcadBySortNum(3);
        Pcadvertising ad4=pcadvertisingService.ReadPcadBySortNum(4);
        Pcadvertising ad5=pcadvertisingService.ReadPcadBySortNum(5);

        System.out.println("ad2"+ad2);

        model.addAttribute("ad1",ad1);
        model.addAttribute("ad2",ad2);
        model.addAttribute("ad3",ad3);
        model.addAttribute("ad4",ad4);
        model.addAttribute("ad5",ad5);

        return "bg/ad";
    }

} // class End
